export function getDataType(data) {
    return Object.prototype.toString.call(data).replace(/\[object (\w+)\]/, (a, b) => b)
}

export function random(min, max) {
    return Math.floor(Math.random() * (max - min) + min)
}

export function selectFile() {
    return new Promise((resolve, reject) => {
        let input = document.createElement('input')
        input.type = 'file'
        input.click()
        input.onchange = function (e) {
            resolve(e.target.files)
        }
    })
}

export function checkEmail(str) {
    let re = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/
    return re.test(str)
}

export function checkPhone(str) {
    let re = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/
    return re.test(str)
}

export function checkIP(str) {
    let re = /^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$/
    return re.test(str)
}

export function checkPort(str) {
    let re = /^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/
    return re.test(str)
}